package com.example.hyw.szysteam.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.presenter.LoginPresenter;
import com.example.hyw.szysteam.utils.CodeTypeCheckUtil;
import com.example.hyw.szysteam.utils.ShowTips;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;


public class MainActivity extends AppCompatActivity {
    Button login;
    EditText email;
    EditText psw;
    TextView forget;
    TextView toRegist;
    RegistDialog registDialog;
    ResetPswDialog resetPswDialog;
    private LoginPresenter loginPresrnter=new LoginPresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        loginPresrnter.getUserInfo(this);
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.translucent(this);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void showRegistDialog(){
        registDialog =new RegistDialog();
        registDialog.show(getSupportFragmentManager(),"Regist");
    }

    public void showResetDialog(){
        resetPswDialog =new ResetPswDialog();
        resetPswDialog.show(getSupportFragmentManager(),"Reset");
    }

    public void initView(){
        login=findViewById(R.id.login_button);
        email=findViewById(R.id.email);
        psw=findViewById(R.id.password);
        forget=findViewById(R.id.forget_psw);
        toRegist=findViewById(R.id.regist);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(CodeTypeCheckUtil.isEmail(email.getText().toString())){
                    loginPresrnter.toLogin(email.getText().toString(),psw.getText().toString());
                }else{
                    ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_FAIL,"请输入邮箱密码",login,MainActivity.this);
                }
            }
        });
        toRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRegistDialog();
            }
        });
        forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showResetDialog();
            }
        });
    }
    public void loginSuccess(){
        final QMUITipDialog tipDialog;
        tipDialog=new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("登陆成功")
                .create();
        tipDialog.show();
        login.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
                nextActivity();
            }
        },1000);
    }

    public void loginFail(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_FAIL,"用户名或密码错误",login,this);
    }

    public void nextActivity(){
        Intent intent=new Intent(MainActivity.this,ViewPagerActivity.class);
        startActivity(intent);
        finish();
    }

    public void loginExpire(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_FAIL,"登陆状态过期",login,this);

    }
}
